package cn.iocoder.yudao.module.distm.controller.admin.schedule;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.distm.controller.admin.schedule.vo.FlightLogImportRespVO;
import cn.iocoder.yudao.module.distm.controller.admin.schedule.vo.ScheduleRecordPageReqVO;
import cn.iocoder.yudao.module.distm.controller.admin.schedule.vo.ScheduleRecordRespVO;
import cn.iocoder.yudao.module.distm.controller.admin.schedule.vo.ScheduleRecordSaveReqVO;
import cn.iocoder.yudao.module.distm.dal.dataobject.schedule.ScheduleRecordDO;
import cn.iocoder.yudao.module.distm.service.schedule.ScheduleRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 配送管理-调度生产记录")
@RestController
@RequestMapping("/distm/schedule-record")
@Validated
public class ScheduleRecordController {

    @Resource
    private ScheduleRecordService scheduleRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建配送管理-调度生产记录")
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:create')")
    public CommonResult<Long> createScheduleRecord(@Valid @RequestBody ScheduleRecordSaveReqVO createReqVO) {
        return success(scheduleRecordService.createScheduleRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配送管理-调度生产记录")
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:update')")
    public CommonResult<Boolean> updateScheduleRecord(@Valid @RequestBody ScheduleRecordSaveReqVO updateReqVO) {
        scheduleRecordService.updateScheduleRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配送管理-调度生产记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:delete')")
    public CommonResult<Boolean> deleteScheduleRecord(@RequestParam("id") Long id) {
        scheduleRecordService.deleteScheduleRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除配送管理-调度生产记录")
                @PreAuthorize("@ss.hasPermission('distm:schedule-record:delete')")
    public CommonResult<Boolean> deleteScheduleRecordList(@RequestParam("ids") List<Long> ids) {
        scheduleRecordService.deleteScheduleRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配送管理-调度生产记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:query')")
    public CommonResult<ScheduleRecordRespVO> getScheduleRecord(@RequestParam("id") Long id) {
        ScheduleRecordDO scheduleRecord = scheduleRecordService.getScheduleRecord(id);
        return success(BeanUtils.toBean(scheduleRecord, ScheduleRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配送管理-调度生产记录分页")
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:query')")
    public CommonResult<PageResult<ScheduleRecordRespVO>> getScheduleRecordPage(@Valid ScheduleRecordPageReqVO pageReqVO) {
        PageResult<ScheduleRecordDO> pageResult = scheduleRecordService.getScheduleRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ScheduleRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配送管理-调度生产记录 Excel")
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScheduleRecordExcel(@Valid ScheduleRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScheduleRecordDO> list = scheduleRecordService.getScheduleRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配送管理-调度生产记录.xls", "数据", ScheduleRecordRespVO.class,
                        BeanUtils.toBean(list, ScheduleRecordRespVO.class));
    }

    @GetMapping("/sync")
    @Operation(summary = "同步季度计划")
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:sync')")
    public CommonResult<Boolean> syncQuarterPlan() {
        scheduleRecordService.syncQuarterPlan();
        return success(true);
    }

    @PostMapping("/import")
    @Operation(summary = "导入航班日志")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('distm:schedule-record:import')")
    public CommonResult<FlightLogImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                             @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        byte[] content = IoUtil.readBytes(file.getInputStream());
        return success(scheduleRecordService.flightLogImport(content, updateSupport));
    }

}